package dao

import (
	"server_cluster/common/gdconf"
	"server_cluster/http/model"
)

func (d *Dao) GetServerListByType(serverType int) ([]*model.DbServer, error) {
	serverList := make([]*model.DbServer, 0)
	err := d.db.Where("`type` = ?", serverType).Find(&serverList).Error
	if err != nil {
		return nil, err
	}
	return serverList, nil
}

func (d *Dao) GetServerListById(serverId int32) (*model.DbServer, error) {
	server := new(model.DbServer)
	err := d.db.Where("`id` = ?", serverId).First(server).Error
	if err != nil {
		return nil, err
	}
	return server, nil
}

func (d *Dao) GetActiveServerListByType(serverType int) ([]*model.DbServer, error) {
	serverList := make([]*model.DbServer, 0)
	err := d.db.
		Where("`type` = ?", serverType).
		Where("`status` = ? ", 1).
		//Where("`stock` != ?", 0).
		Find(&serverList).Error
	if err != nil {
		return nil, err
	}
	return serverList, nil
}

func (d *Dao) GetAccountRegisterCount() (int64, error) {
	var count int64
	err := d.db.Model(&model.DbAccount{}).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return count, nil
}

func (d *Dao) GetServerStartTime() (int64, error) {
	return gdconf.GetServerStartTime(d.db), nil
}
